/**
 * Yun
 * 2016-11-23
 * 组件 基类 demo 请参照 BaseUI.js 文件
 */
'use strict';
import React, {Component} from 'react';
import {
  BackAndroid,
  Platform,
  Alert,
} from 'react-native';
const dismissKeyboard = require('dismissKeyboard');

export default class BaseComponent extends Component {


  componentWillMount() {
    global.alert = function(msg){
      Alert.alert(msg);
    };
    if (Platform.OS === 'android') {
      BackAndroid.addEventListener('hardwareBackPress', this.onBackAndroid);
    }
    dismissKeyboard();
  }

  componentWillUnmount() {
    if (Platform.OS === 'android') {
      BackAndroid.removeEventListener('hardwareBackPress', this.onBackAndroid);
    }
  }

  onBackAndroid = () => {
    const { navigator } = this.props;
    const routers = navigator.getCurrentRoutes();
    // console.log('当前路由长度：'+routers.length);
    if (routers.length > 1) {
      navigator.pop();
      return true;//接管默认行为
    }
    return false;//默认行为
  };
}
